import { Component, OnDestroy, OnInit } from "@angular/core";
import { sjgzxinModuledatas } from "../pubcomponent/pageTitle.datas";
import { CommService } from "../comm-service";
import { LocalStorageService } from "../local-storage-service";
import { Router } from "@angular/router";
import { MyhttpService } from "../myhttp.service";

declare var $: any

@Component({
  template: `
  <div class="all_bg paddingTop">
    <div class="">
      <div class="marginBottom">
        <ng-container *ngIf="actionobj1.length">
          <portlet-action class="actions" [o]="actionobj1" (outhander)="actionHander($event)"></portlet-action>
        </ng-container>
      </div>
      <div class="portlet-body">
        <div class="row">
          <form class="form-horizontal" role="form">
            <div class="col-lg-4 col-lg-offset-7">
              <div class="form-group">
                <label class="col-lg-3 control-label">运送计划名称：</label>
                <div class="col-lg-8">
                  <input type="text" class="form-control" name="ysjhmc" [(ngModel)]="s_obj.ysjhmc"/>
                </div>
              </div>
            </div>
            <div class="col-lg-1">
              <button type="button" class="btn blue" (click)="inittable_yyjh()">
                <i class="fa fa-search"></i> 搜索
              </button>
            </div>
          </form>
        </div>
        <div class="row">
          <div class="col-lg-12" *ngIf="table_yyjh">
            <my-table [o]="table_yyjh"></my-table>
          </div>
        </div>
      </div>
    </div>
    </div>
  `
})
export class YyjhComponent implements OnInit, OnDestroy {
  userinfo: any;
  actionobj1: any[] = [];
  yyjh_btns: any[] = ["yyjh_add", "yyjh_removeN", "yyjh_refresh"];
  s_obj: any = {
    ysjhmc: "",
    ksjhbh: ""
  }
  table_yyjh: any;
  yyjhztKeyValue: any = {
    "1": "已完成",
    "2": "未完成",
    "3": "未开始"
  }

  /**
   * 初始化按钮
   */
  initactions(): void {
    // this.commservice.getroleBtns(this.yyjh_btns, [], this.userinfo.nowroleid, "tableyyjh", (hasportbtns, hastablebtns) => {
    //   this.actionobj1 = hasportbtns;
    // });
    this.commservice.getroleBtns_not_permissions(this.yyjh_btns, [], "tableyyjh", (hasportbtns, hastablebtns) => {
      this.actionobj1 = hasportbtns;
    });
  }

  /**
   * 按钮的处理回调
   * @param e
   */
  actionHander(e: any): void {
    const fromv = e.pfrom;
    const actiontype = e.type;
    if (fromv === "tableyyjh") {
      if (actiontype === "refresh") {
        // 刷新
        this.table_yyjh = Object.assign({}, this.table_yyjh);
      } else if (actiontype == "add") {
        // 新增加
        this.router.navigate(["sjgzxin.jcsj/xxglx", "yyjhx.add"]);
      } else if (actiontype === "removeN") {
        // 批量删除
        const selectrows = $("#table_yyjh").bootstrapTable("getSelections");
        if (selectrows.length == 0) {
          this.commservice.myswal("warning", "请选择删除的数据", "", "ts", null);
          return;
        }
        this.commservice.myswal("warning", "确认删除?", "", "cz", () => {
          const ysjhbhList = selectrows.map((item) => {
            return item.ysjhbh;
          });
          this.removeN_yyjh(ysjhbhList);
        });
      }
    }
  }

  /**
   * 批量删除押运计划
   */
  removeN_yyjh(bhs: any): void {
    const url = "sjgz/rwjh/scsjysjh";
    this.itservice.kwPtPost(url, {
      ysjhbhList: bhs
    }).subscribe(resp => {
      this.commservice.handResponse("put", url, resp, (arg1, resp, arg2) => {
        if (arg1) {
          if (arg2) {
            this.commservice.myswal("success", resp, "", "ts", null);
            this.table_yyjh = Object.assign({}, this.table_yyjh);
          } else {
            this.commservice.myswal("warning", resp, "", "ts", null);
          }
        } else {
          this.removeN_yyjh(bhs);
        }
      });
    })
  }

  /**
   * 押运计划表格
   */
  inittable_yyjh(): void {
    const that = this;
    this.table_yyjh = {
      tableid: "table_yyjh",
      url: "sjgz/rwjh/cxsjysjhlb",
      reqobj: this.s_obj,
      sidePagination: "server",
      pageSize: 10,
      pageList: [10, 20, 30],
      total: "totalRows",
      rows: "sjysjhList",
      columns: [{
        checkbox: true
      }, {
        title: "序号",
        field: "ordernum",
        cellStyle: function () {
          return {
            classes: 'td1'
          }
        }
      }, {
        title: "运送计划名称",
        field: "ysjhmc",
        cellStyle: function () {
          return {
            classes: 'td2'
          }
        }
      }, {
        title: "计划开始时间",
        field: "jhkssj",
        cellStyle: function () {
          return {
            classes: 'td3'
          }
        }
      }, {
        title: "计划结束时间",
        field: "jhjssj",
        cellStyle: function () {
          return {
            classes: 'td4'
          }
        }
      }, {
        title: "计划状态",
        field: "jhzt",
        cellStyle: function () {
          return {
            classes: 'td1'
          }
        },
        formatter: function (v) {
          return that.yyjhztKeyValue[v] ? that.yyjhztKeyValue[v] : '';
        }
      }, {
        title: "创建机构",
        field: "cjjgmc",
        cellStyle: function () {
          return {
            classes: 'td2'
          }
        }
      }]
    };
    // this.table_yyjh.pageSize = 50;
    // this.table_yyjh.pageList = [50];
    // this.table_yyjh.hiddenPageinfoLink = true;
    // this.table_yyjh.paginationLoop = true;
    // this.table_yyjh.paginationPreText = "<i class='fa fa-arrow-left'></i>上一页";
    // this.table_yyjh.paginationNextText = "下一页<i class='fa fa-arrow-right'></i>";
    // this.table_yyjh.height = this.commservice.getViewPort().height - this.commservice.topH - this.commservice.topmenuH - 240 - 50;
    this.table_yyjh.height = this.commservice.getViewPort().height - this.commservice.topH - this.commservice.topmenuH - 240 - 50;
  }

  /**
   * 获取用户信息
   */
  getuserinfo(): void {
    this.commservice.getUserinfo((info) => {
      this.userinfo = info;
      this.initactions(); //用户按钮权限
    });
  }


  constructor(private commservice: CommService,
    private localStorageService: LocalStorageService,
    private router: Router,
    private itservice: MyhttpService) {

  }

  ngOnInit() {
    this.getuserinfo();
    this.s_obj.ksjhbh = this.localStorageService.get("hksjh");
    this.inittable_yyjh();

  }

  ngOnDestroy() {

  }

}
